package com.veeder.datacollection.log;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.ConsoleHandler;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

public class SuperLogger {
	public final static String LOGGER_NAME = "com.veeder.datacollection";

	public static final Level DEFAULT_LOG_LEVEL = Level.INFO;

	private static final java.util.logging.Formatter PLAIN_FORMATTER;
	private final static Logger superLogger;

	static {
		PLAIN_FORMATTER = new PlainTextFormatter();
		superLogger = Logger.getLogger(LOGGER_NAME);

		superLogger.setUseParentHandlers(false);
		superLogger.setLevel(DEFAULT_LOG_LEVEL);

		Handler defaultHandler = new ConsoleHandler();
		defaultHandler.setFormatter(PLAIN_FORMATTER);

		superLogger.addHandler(defaultHandler);

		try {
			Handler defaultFileHandler = createFileHandler(LOGGER_NAME);
			defaultFileHandler.setFormatter(PLAIN_FORMATTER);
			defaultFileHandler.setLevel(DEFAULT_LOG_LEVEL);
			superLogger.addHandler(defaultFileHandler);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Logger getSuperLogger() {
		return superLogger;
	}

	private static Handler createFileHandler(String logFileName) throws IOException {
		String ds = new SimpleDateFormat("yyMMddkkmmss").format(new Date());
		String fileName = System.getProperty("user.dir")+ "/logs/" +logFileName + ds + ".log";
		return new FileHandler(fileName, 10485760, 5);
	}
}
